import Vue from 'vue'
import Vuex from 'vuex'
import {select_user} from "../request/api";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
      userList:[],
      myselfAttribute:{},
      websocket:null,
      userRequestMe:[],
      friendList:[],
      bus:new Vue(),//中央事件总线
      messageList:null,
  },
  mutations: {

       setUserList(state,list){
         state.userList=list;
       },

       setMySelfAttribute(state,attribute){
         state.myselfAttribute=attribute;
       },

       setWebSocket(state,websocket){
           state.websocket=websocket;
       },

       cleanWebSocket(state){
           state.websocket=null;
       },
       setUserRequestMe(state,data){
         state.userRequestMe=data;
       },
       setFriendList(state,data){
         state.friendList=data;
       },
       setBus(state,data){
           state.bus=data;
       },
       setMessageList(state,data){
           // state.messageList=data;
           if(data === null){
               state.messageList=null;
           }else{
               let hash = {};
               data = data.reduce(function(item, next) {
                   hash[next.toid] ? '' : hash[next.toid] = true && item.push(next);
                   return item
               }, [])
               state.messageList=data;
           }
       }
  },
  actions: {
      requertUserList(store){
          select_user(null).then(res=>{
              store.commit("setUserList",res.data);
          })
      },
  },

  modules: {

  }
})
